package com.editor.controller;

import com.editor.domain.Ueditor;
import com.editor.util.PicUpload;
import com.editor.util.PublicMsg;
import org.apache.commons.io.FilenameUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by 13994 on 2018/1/30.
 */
@Controller
@RequestMapping("/")
public class editorController {

    @RequestMapping("/")
    private String showPage() {
        return "demo";
    }

    @RequestMapping("/1")
    private String showPage1() {
        return "index";
    }

    @RequestMapping("/2")
    private String showPage2() {
        return "updatenews";
    }

    @RequestMapping(value = "/ueditor")
    @ResponseBody
    public String ueditor(HttpServletRequest request) {

        return PublicMsg.UEDITOR_CONFIG;
    }

//    @RequestMapping(value = "/imgUpload")
//    @ResponseBody
//    public Ueditor imgUpload(MultipartFile upfile) {
//        Ueditor ueditor = new Ueditor();
//        return ueditor;
//    }

//    @RequestMapping(value = "/imgUpload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//    @ResponseBody
//    public Map<String,Object> save(HttpServletRequest req){
//        Map<String,Object> rs = new HashMap<String, Object>();
//        MultipartHttpServletRequest mReq  =  null;
//        MultipartFile file = null;
//        String fileName = "";
//        // 原始文件名   UEDITOR创建页面元素时的alt和title属性
//        String originalFileName = "";
//        try {
//            mReq = (MultipartHttpServletRequest)req;
//            // 从config.json中取得上传文件的ID
//            file = mReq.getFile("upfile");
//
//            if(!file.isEmpty()){
//                // 取得文件的原始文件名称
//                fileName = file.getOriginalFilename();
//                originalFileName = fileName;
//
//                String ext = (FilenameUtils.getExtension(file.getOriginalFilename())).toLowerCase();
//                String storePath = "";
//                if ("jpg".equals(ext) || "png".equals(ext) || "jpeg".equals(ext) || "bmp".equals(ext)) {
//                    storePath = "upload/image/";
//                }else{
//                    storePath = "upload/video/";
//                }
//                //将图片和视频保存在本地服务器
//                String pathRoot = req.getSession().getServletContext().getRealPath("");
//                String path = pathRoot + "/" + storePath;
//
//                file.transferTo(new File(path+fileName));
//                String httpImgPath = storePath + fileName;
//
//                rs.put("state", "SUCCESS");// UEDITOR的规则:不为SUCCESS则显示state的内容
//                rs.put("url",httpImgPath);         //能访问到你现在图片的路径
//                rs.put("title", originalFileName);
//                rs.put("original", originalFileName);
//            }
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            rs.put("state", "文件上传失败!"); //在此处写上错误提示信息，这样当错误的时候就会显示此信息
//            rs.put("url","");
//            rs.put("title", "");
//            rs.put("original", "");
//        }
//        return rs;
//    }

//
//    @RequestMapping(value = "/imgUpload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//    @ResponseBody
//    public Map<String,Object> save(HttpServletRequest req){
//        String imgResouse = "E:\\upload\\img\\";
//        Map<String,Object> rs = new HashMap<String, Object>();
//        MultipartHttpServletRequest mReq  =  null;
//        MultipartFile file = null;
//        String fileName = "";
//        // 原始文件名   UEDITOR创建页面元素时的alt和title属性
//        String originalFileName = "";
//        try {
//            mReq = (MultipartHttpServletRequest)req;
//            // 从config.json中取得上传文件的ID
//            file = mReq.getFile("upfile");
//
//            if(!file.isEmpty()){
//                // 取得文件的原始文件名称
//                fileName = file.getOriginalFilename();
//                originalFileName = fileName;
//
////                String ext = (FilenameUtils.getExtension(file.getOriginalFilename())).toLowerCase();
////                String storePath = "";
////                if ("jpg".equals(ext) || "png".equals(ext) || "jpeg".equals(ext) || "bmp".equals(ext)) {
////                    storePath = "upload/image/";
////                }else{
////                    storePath = "upload/video/";
////                }
//                //将图片和视频保存在本地服务器
////                String pathRoot = req.getSession().getServletContext().getRealPath("");
////                String path = pathRoot + "/" + storePath;
//
//                File dest = new File(imgResouse+ fileName);
//                if (!dest.getParentFile().exists()) {
//                    dest.getParentFile().mkdirs();
//                }
//
//                file.transferTo(dest);
//                String httpImgPath = imgResouse+ fileName;
//
//                rs.put("state", "SUCCESS");// UEDITOR的规则:不为SUCCESS则显示state的内容
//                rs.put("url",httpImgPath);         //能访问到你现在图片的路径
//                rs.put("title", originalFileName);
//                rs.put("original", originalFileName);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            rs.put("state", "文件上传失败!"); //在此处写上错误提示信息，这样当错误的时候就会显示此信息
//            rs.put("url","");
//            rs.put("title", "");
//            rs.put("original", "");
//        }
//        return rs;
//    }


    @RequestMapping(value = "/imgUpload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest req) {
        Map<String, Object> rs = new HashMap<String, Object>();
        MultipartHttpServletRequest mReq = null;
        MultipartFile file = null;
        String fileName = "";
        // 原始文件名   UEDITOR创建页面元素时的alt和title属性
        mReq = (MultipartHttpServletRequest) req;
        // 从config.json中取得上传文件的ID
        file = mReq.getFile("upfile");
        String picId= UUID.randomUUID().toString().replaceAll("-", "");

        String destUrl="/shuidian2/picture/MicroWebPic/p"+picId+".jpg";

        File destFile=new File(destUrl);

        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String httpImgPath = "http:www.wchenglearning.xyz/shuidian2/MicroWebPic/p"+picId+".jpg";
        rs.put("state", "SUCCESS");// UEDITOR的规则:不为SUCCESS则显示state的内容
        rs.put("url", httpImgPath);         //能访问到你现在图片的路径
        return rs;

    }
}